import { _decorator, Component, Node, Prefab, Vec3, CCFloat } from 'cc';
import { poolManager } from '../../../../Framework/Scripts/Managers/poolManager';
import { GameApp } from '../../GameApp';
import { enemyBase } from '../enemyBase';
import { boss_aoeBomb } from '../enemySkill/boss_aoeBomb';
const { ccclass, property } = _decorator;


/** 
  周无邪出品
  本源码用于学习研究使用。
  作者联系方式：tuya7078（微信）。
 */
@ccclass('boomDragon')
export class boomDragon extends enemyBase {

    @property({ type: Prefab, displayName: "狂暴技能预制体" })
    protected skillPrefab: Prefab = null
    @property({ type: Prefab, displayName: "狂暴技能预警" })
    protected skillWaming: Prefab = null
    @property({ type: Number, displayName: "狂暴百分比" })
    protected kuangbao: number = 0.5
    @property({ type: CCFloat, displayName: "狂暴后的攻击范围" })
    protected kuangbaoAttackRange: number = 20






    private nowSwaming: Node = null



    private isA: boolean = false//怪物正在攻击





    private atime: number = 3
    private nowTime: number = 0


    onLoad() {

            super.onLoad()
     
        this.kuangbaoAttackRange *= this.kuangbaoAttackRange
    }

    start() {

    }
    pursuit_building() {
        this.isA = false
        if (this.nowSwaming) {
            this.nowSwaming.active = false
        }
        super.pursuit_building()
    }

    enemy_pursuit() {
        this.isA = false
        if (this.nowSwaming) {
            this.nowSwaming.active = false
        }
        super.enemy_pursuit()
    }

    friendly_pursuit() {
        this.isA = false
        if (this.nowSwaming) {
            this.nowSwaming.active = false
        }
       super.friendly_pursuit()
    }
    hit(num: number,how:Node) {
        super.hit(num,how)
        if (this.hp / this.totalHP <= this.kuangbao) {
            this.isKuangbao = true
            this.nowAttackRange = this.kuangbaoAttackRange
        }




    }
    die() {
        this.isA = false
        if (this.nowSwaming) {
            this.nowSwaming.active = false
        }
        super.die()
    }

    idle() {
        //  this.unschedule(this.fire.bind(this))
        this.isA = false
        if (this.nowSwaming) {
            this.nowSwaming.active = false
        }
        super.idle()

    }
    attack() {
        super.attack()

        if (this.isKuangbao) {
            //如果是狂暴了
            if (this.isA) {
                //    console.log("正在瞄准");

                return
            }
            this.isA = true
            this.node.lookAt(this.attackTarget.getWorldPosition())

            //   this.unschedule(this.fire.bind(this))



            //   this.enemyModel.playAni(this.idelAni, true)

            if (!this.nowSwaming) {
                this.nowSwaming = poolManager.instance.getNode(this.skillWaming, GameApp.Instance.wrramingRoot)


            }

            this.nowSwaming.active = true
            this.nowSwaming.setWorldPosition(this.node.worldPosition.x, 0, this.node.worldPosition.z)



            this.nowSwaming.lookAt(this.attackTarget.worldPosition)



            //   this.scheduleOnce(this.fire.bind(this), 3)
        } else {
         //   this.NavTs.StopNav()
            this.node.lookAt(this.attackTarget.getWorldPosition())
            this.enemyModel.playAni(this.attackAni1, true)
        }


    }

    attickCallback() {
        if (this.isKuangbao) {
            if (!this.nowSwaming) {
                return
            }
            const k: Node = poolManager.instance.getNode(this.skillPrefab, GameApp.Instance.wrramingRoot)
            k.setWorldPosition(this.nowSwaming.worldPosition.x, 1, this.nowSwaming.worldPosition.z)
            const f: Vec3 = this.nowSwaming.forward
            k.forward = new Vec3(f.x, 0, f.z)
            const ts: boss_aoeBomb = k.getComponent(boss_aoeBomb)
            if (ts) {
                ts.init(this.node)
            }
            this.nowSwaming.active = false
            this.isA = false



        } else {
            this.putonggognji()
        }

    }//攻击回调

    // private fire() {
    //     this.NavTs.StopNav()
    //     this.enemyModel.playAni(this.attackAni1, false,()=>{
    //         this.enemyModel.playAni(this.idelAni,true)
    //     })
    // }
    update(dt: number) {

        if (this.isA) {
            this.nowTime += dt
            if (this.nowTime >= this.atime) {
                this.nowTime = 0

                this.NavTs.StopNav()
                this.enemyModel.playAni(this.attackAni1, false, () => {
                    this.isA = false
                    this.NavTs.StopNav()
                    this.enemyModel.playAni(this.walkAni, true)
                })
            }
        }

        super.update(dt)
    }
}


